//
//  WaterflowLayout.m
//  蘑菇街瀑布流
//
//  Created by app15 on 15/7/14.
//  Copyright (c) 2015年 cn.scjy. All rights reserved.
//

#import "WaterflowLayout.h"
#import "DSZTJCollectionViewCell.h"
#import "MJExtension.h"
#import "DSZTJGroupModel.h"
#import "DSZTJCommentModel.h"
#import "DSZTJShowModel.h"
CGFloat h;
@interface WaterflowLayout ()

@property(nonatomic,strong)NSMutableDictionary *maxYDict;//最大的高
@property(nonatomic,strong)NSMutableArray *attrsArray;//属性数组
@property(nonatomic,strong)NSMutableArray *CommentNumArray;//每个show评价数的数组
@end
@implementation WaterflowLayout
//最大的高
-(NSMutableDictionary*)maxYDict{
    if (!_maxYDict) {
        self.maxYDict=[[NSMutableDictionary alloc]init];
    }
    return _maxYDict;
}

//属性数组
-(NSMutableArray*)attrsArray{
    if (!_attrsArray) {
        self.attrsArray=[[NSMutableArray alloc]init];
    }
    return _attrsArray;
}

//每个show评价数的数组
-(NSMutableArray*)attrsArray:(NSArray*)array{
    _CommentNumArray=[NSMutableArray array];
    for (DSZTJGroupModel *model in array) {
        NSDictionary *dic =[model keyValues];
        NSArray *array=dic[@"show"];
        
        NSArray *showarray=[DSZTJShowModel modelArrayWithDictArray:array];
        DSZTJShowModel *showmodel=showarray[0];
        
        NSDictionary *commentdic=[showmodel keyValues];
        NSArray *commentarray=commentdic[@"comment"];
        NSArray *commentArrayModel=[DSZTJCommentModel modelArrayWithDictArray:commentarray];
        int commentNum=commentArrayModel.count;
        NSString *str=[NSString stringWithFormat:@"%i",commentNum];
        [_CommentNumArray addObject:str];
    }
    return _CommentNumArray;
}

//重写初始化方法
-(instancetype)init{
    if (self=[super init]) {
        //self.columnMargin=10;
        self.rowMargin=10;
        self.columnCount=1;
        self.sectioninset=UIEdgeInsetsMake(0, 0, 15, 0);
    }
    return self;
}

-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    return YES;
}

-(void)prepareLayout{
    [super prepareLayout];
    for (int i=0; i<self.columnCount; i++) {
        if (i==1) {
            NSString *column=[NSString stringWithFormat:@"%d",i];
            self.maxYDict[column]=@(self.sectioninset.top+180);
        }else{
            NSString *column=[NSString stringWithFormat:@"%d",i];
            self.maxYDict[column]=@(self.sectioninset.top);
        }
    }
    
    [self.attrsArray removeAllObjects];
    NSInteger count=[self.collectionView numberOfItemsInSection:0];
    for (int i=0; i<count; i++) {
        UICollectionViewLayoutAttributes *attr=[self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
        [self.attrsArray addObject:attr];
    }
   // NSLog(@"%f",h);
    //self.collectionView.collectionViewLayout.collectionViewContentSize=CGSizeMake(0, h);

    //self.collectionView.contentSize=CGSizeMake(self.collectionView.frame.size.width, h);
}

//设置最大y
-(CGSize)collectionViewContentSize{
    __block NSString *maxColumn=@"0";
    [self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column,NSNumber *maxY,BOOL *stop){
        if ([maxY floatValue]>[self.maxYDict[maxColumn] floatValue]) {
            maxColumn=column;
        }
    }];
    return CGSizeMake(0, [self.maxYDict[maxColumn] floatValue]+self.sectioninset.bottom);
}


//每个item的属性
-(UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{

    __block NSString *minColumn=@"0";
    
    [self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column,NSNumber *maxY,BOOL *stop){
        if ([maxY floatValue]<[self.maxYDict[minColumn] floatValue]) {
            minColumn=column;
        }
    }];
    
    //设置item的frame
    //width=(320-20-(2-1)*10)/2
    CGFloat width=self.collectionView.frame.size.width;
    CGFloat height;
    //得到每个Item的高
    if ([_CommentNumArray[indexPath.row] intValue]<3) {
        height=604-(3-[_CommentNumArray[indexPath.row] intValue])*44;
    }else{
        height=604+44;
    }
    CGFloat X=0;
    CGFloat Y=[self.maxYDict[minColumn] floatValue]+self.rowMargin;
    
    //最大的y
    self.maxYDict[minColumn]=@(Y+height);
    h=Y+height;
    
    UICollectionViewLayoutAttributes *attr=[UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    attr.frame=CGRectMake(X, Y, width, height);
    return attr;
}

-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect{
    
    return self.attrsArray;
}

@end
